package com.ruantu.clothing.service.report;

import com.ruantu.clothing.dao.report.RukuReportDao;
import com.ruantu.clothing.entity.PageBean;
import com.ruantu.clothing.entity.report.Rukuyuebao;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RukuReportServiceImpl implements RukuReportService {
    @Autowired
    private RukuReportDao rukuReportDao;


    public String gtRukuyuebaoday(String monSearch, String selectText, String jgid) {
        List<Rukuyuebao> ruKuSonXs = rukuReportDao.gtRukuyuebao(monSearch,selectText,jgid);
        List<Rukuyuebao> ruku = new ArrayList<Rukuyuebao>();
        for (int i = 0; i < ruKuSonXs.size(); i++) {
            if (i != 0 && ruKuSonXs.get(i).getProduct().equals(ruKuSonXs.get(i - 1).getProduct())) {
                continue;
            } else {
                Rukuyuebao rukuyuebao = new Rukuyuebao();
                rukuyuebao.setProduct(ruKuSonXs.get(i).getProduct());
                rukuyuebao.setProductName(ruKuSonXs.get(i).getProductName());
                rukuyuebao.setPinpai(ruKuSonXs.get(i).getPinpai());
                //System.out.println(rukuyuebao);
                ruku.add(rukuyuebao);
            }
        }

        String jsonStr = "[";
        for (int i = 0; i < ruku.size(); i++) {
            int sum = 0;
            String pingjie = "{product:\"" + ruku.get(i).getProduct() + "\",";
            pingjie+="productName:\"" + ruku.get(i).getProductName() + "\",";
            pingjie+="pinpai:\"" + ruku.get(i).getPinpai() + "\",";
            for (int j = 0; j < ruKuSonXs.size(); j++) {
                if (ruku.get(i).getProduct().equals(ruKuSonXs.get(j).getProduct())) {
                    String shijian = ruKuSonXs.get(j).getRukushijian().toString();
                    int riqi = Integer.parseInt(shijian.substring(8, 10));
                    int shuliang = ruKuSonXs.get(j).getRkshu();
                    sum += ruKuSonXs.get(j).getRkshu();
                    pingjie += "shuliang" + riqi + ":" + shuliang + ",";
                }
            }
            jsonStr += pingjie + "zongji:" + sum + "},";
            ruku.get(i).setRkshu(sum);
        }
        if (jsonStr.length() > 1) {
            jsonStr = jsonStr.substring(0, jsonStr.length() - 1);
        }

        jsonStr += "]";
        return jsonStr;
    }

    /**
     * 入库报表
     * @param stime
     * @param etime
     * @param jgid
     * @param selectText
     * @param pageBean
     * @return
     */
    public  List<Rukuyuebao> gtrukureport(String stime, String etime, String jgid, String selectText,
                                          String cpYear,String season,String pinpai,Integer ordTJ,PageBean pageBean){
        pageBean.setTotal(rukuReportDao.gtrukureportTotal(stime,etime,jgid,selectText,cpYear,season,pinpai,ordTJ));
        return rukuReportDao.gtrukureport(stime,etime,jgid,selectText,cpYear,season,pinpai,ordTJ,pageBean.getStart(),pageBean.getRows());
    }

    /**
     * 合计
     * @param stime
     * @param etime
     * @param jgid
     * @return
     */
    public  List<Rukuyuebao> geAllTotalByTime(String stime,String etime, String selectText, String cpYear,String season,String pinpai,String jgid){
        List<Rukuyuebao> Rukuyuebaos = new ArrayList<>();
        Rukuyuebao rukuyuebao=new Rukuyuebao();
        rukuyuebao.setProduct("总计：");
        rukuyuebao.setRkshu(rukuReportDao.geAllTotalByTime(stime,etime,selectText,cpYear,season,pinpai,jgid));
        Rukuyuebaos.add(rukuyuebao);
        return Rukuyuebaos;
    }

    /**
     * 导出入库月表（未完成）
     * @param monSearch
     * @param selectText
     * @param jgid
     * @return
     */
    public XSSFWorkbook gtRukuyuebaoExcel(String monSearch, String selectText,String jgid){
        List<Rukuyuebao> rks=rukuReportDao.gtRukuyuebao(monSearch,selectText,jgid);
        System.out.println(rks);
        List<Rukuyuebao> ruku = new ArrayList<Rukuyuebao>();
        for (int i = 0; i < rks.size(); i++) {
            if (i != 0 && rks.get(i).getProduct().equals(rks.get(i - 1).getProduct())) {
                continue;
            } else {
                Rukuyuebao rukuyuebao = new Rukuyuebao();
                rukuyuebao.setProduct(rks.get(i).getProduct());
                rukuyuebao.setProductName(rks.get(i).getProductName());
                rukuyuebao.setPinpai(rks.get(i).getPinpai());
                ruku.add(rukuyuebao);
            }
        }

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("sheet1");
        //表头样式
        CellStyle titleStyle = wb.createCellStyle();
        titleStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
        titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        XSSFFont font = wb.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        titleStyle.setFont(font);
        // 设置这些样式
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("商品款号");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(1);
        cell.setCellValue("数量");
        cell.setCellStyle(titleStyle);
        int yue=30;
        for (int i=1;i<=yue+1;i++){
            cell = row.createCell(i+1);
            cell.setCellValue(i);
            cell.setCellStyle(titleStyle);
        }
        cell = row.createCell(yue+1);
        cell.setCellValue("商品名称");
        cell.setCellStyle(titleStyle);
        sheet.setColumnWidth(yue+1, 5200);
        cell = row.createCell(yue+2);
        cell.setCellValue("品牌");
        cell.setCellStyle(titleStyle);
        CellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//
        for (int i = 0; i < ruku.size(); i++) {
            int sum = 0;
            XSSFRow row2 = sheet.createRow(i+2);
            for (int j = 0; j < rks.size(); j++) {
                if (ruku.get(i).getProduct().equals(rks.get(j).getProduct())) {
                    sum += rks.get(j).getRkshu();
                }
            }
            for (int j=0;j<yue;j++){
                row2.createCell(j+2).setCellValue(rks.get(i).getRkshu());
            }
            //System.out.println("sum:"+sum);
            row2.createCell(0).setCellValue(rks.get(i).getProduct());
            row2.createCell(1).setCellValue(sum);
            /*for (int j=0;j<yue;j++){
                row2.createCell(j+2).setCellValue(rks.get(i).getRkshu());
            }*/
            row2.createCell(yue+1).setCellValue(rks.get(i).getProductName());
            row2.createCell(yue+2).setCellValue(rks.get(i).getPinpai());

        }
        return wb;
    }

    /**
     * 导出入库报表
     * @param stime
     * @param etime
     * @param selectText
     * @param ordTJ
     * @param jgid
     * @return
     */
    public XSSFWorkbook download(String stime, String etime, String selectText, String cpYear,String season,String pinpai,Integer ordTJ, String jgid) {
        List<Rukuyuebao> rks=rukuReportDao.gtrukureport2(stime,etime,jgid,selectText,cpYear,season,pinpai,ordTJ);
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("sheet1");
        //表头样式
        CellStyle titleStyle = wb.createCellStyle();
        titleStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index);
        titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        XSSFFont font = wb.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        titleStyle.setFont(font);
        // 设置这些样式
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("商品款号");
        cell.setCellStyle(titleStyle);
        sheet.setColumnWidth(0, 4500);
        cell = row.createCell(1);
        cell.setCellValue("名称");
        cell.setCellStyle(titleStyle);
        sheet.setColumnWidth(1, 4000);
        cell = row.createCell(2);
        cell.setCellValue("年份");
        cell.setCellStyle(titleStyle);
        cell = row.createCell(3);
        cell.setCellValue("季节");
        cell.setCellStyle(titleStyle);
        if (ordTJ==0||"0".equals(ordTJ)){
            cell = row.createCell(4);
            cell.setCellValue("数量");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(5);
            cell.setCellValue("品牌");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(6);
            cell.setCellValue("入库时间");
            cell.setCellStyle(titleStyle);
            sheet.setColumnWidth(6, 5200);
        }
        if (ordTJ==1||"1".equals(ordTJ)){
            cell = row.createCell(4);
            cell.setCellValue("颜色");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(5);
            cell.setCellValue("数量");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(6);
            cell.setCellValue("品牌");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(7);
            cell.setCellValue("入库时间");
            cell.setCellStyle(titleStyle);
            sheet.setColumnWidth(7, 5200);
        }
        if (ordTJ==2||"2".equals(ordTJ)){
            cell = row.createCell(4);
            cell.setCellValue("颜色");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(5);
            cell.setCellValue("尺码");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(6);
            cell.setCellValue("数量");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(7);
            cell.setCellValue("品牌");
            cell.setCellStyle(titleStyle);
            cell = row.createCell(8);
            cell.setCellValue("入库时间");
            cell.setCellStyle(titleStyle);
            sheet.setColumnWidth(8, 5200);

        }

        CellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//

     /* for (int i = 0; i <= rks.size(); i++) {
            if (i == rks.size()-1) {
               sheet.setColumnWidth(i, 4000);
           } else {
                sheet.setColumnWidth(i, 3500);

             }
        }*/
        CellStyle titleStyle2 = wb.createCellStyle();
        titleStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        titleStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        System.out.println(rks);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date=new Date();
        //创建单元格（excel的单元格，参数为列索引，可以是0～255之间的任何一个
        XSSFRow rowh = sheet.createRow(1);
        XSSFCell cell2 = rowh.createCell(0);
        //设置单元格内容
       // cell2.setCellValue("导出时间:"+sdf.format(date));
        cell2.setCellValue("报表周期:"+stime+"-—-"+etime);
        cell2.setCellStyle(titleStyle2);
        int lastCol=0;
        if(ordTJ==0||"0".equals(ordTJ)){
            lastCol=6;

        }
        if (ordTJ==1||"1".equals(ordTJ)){
            lastCol=7;

        }
        if (ordTJ==2||"2".equals(ordTJ)){
            lastCol=8;
        }
        sheet.addMergedRegion(new CellRangeAddress(1,1,0,lastCol));
        for (int i = 0; i < rks.size(); i++){
            String rksj=sdf.format(rks.get(i).getRukushijian());
            XSSFRow row2 = sheet.createRow(i+2);
            //cell.setCellStyle(style);// 设置样式
            row2.createCell(0).setCellValue(rks.get(i).getProduct());
            row2.createCell(1).setCellValue(rks.get(i).getProductName());
            row2.createCell(2).setCellValue(rks.get(i).getCpYear());
            row2.createCell(3).setCellValue(rks.get(i).getSeason());
            if(ordTJ==0||"0".equals(ordTJ)){
                row2.createCell(4).setCellValue(rks.get(i).getRkshu());
                row2.createCell(5).setCellValue(rks.get(i).getPinpai());
                row2.createCell(6).setCellValue(rksj);
            }
            if (ordTJ==1||"1".equals(ordTJ)){
                row2.createCell(4).setCellValue(rks.get(i).getColor());
                row2.createCell(5).setCellValue(rks.get(i).getRkshu());
                row2.createCell(6).setCellValue(rks.get(i).getPinpai());
                row2.createCell(7).setCellValue(rksj);
            }
            if (ordTJ==2||"2".equals(ordTJ)){
                row2.createCell(4).setCellValue(rks.get(i).getColor());
                row2.createCell(5).setCellValue(rks.get(i).getSize());
                row2.createCell(6).setCellValue(rks.get(i).getRkshu());
                row2.createCell(7).setCellValue(rks.get(i).getPinpai());
                row2.createCell(8).setCellValue(rksj);
            }
        }
      return wb;
    }
}

